<!-- 需求分析 -->
<template>

	<ListLayout>

		<Card>
			<template #head>
				需求分析信息
			</template>
			<template #body>
				<evan-form :model="formData" ref="form">
					<evan-form-item label="需求分析人" prop="analyst">
						<ItemRow v-model="formData.analyst" ref="analyst" chooseType="select-user"></ItemRow>
					</evan-form-item>
					<evan-form-item label="需求类型" prop="type">
						<ItemRow v-model="formData.type" dicKey="itemType"></ItemRow>
					</evan-form-item>
					<evan-form-item label="核心需求" prop="coreDemand">
						<u-input v-model="formData.coreDemand" type="textarea" input-align="right"></u-input>
					</evan-form-item>
					<evan-form-item label="需求价值" prop="demandValue">
						<u-input v-model="formData.demandValue" type="textarea" input-align="right"></u-input>
					</evan-form-item>
					<evan-form-item label="技术分析人" prop="analystTechnical">
						<ItemRow v-model="formData.analystTechnical" chooseType="select-user"></ItemRow>
					</evan-form-item>
					<evan-form-item label="涉及业务领域" prop="domain">
						<ItemRow v-model="formData.domain" dicKey="coverageInvolved" chooseType="select-dic-multiple"></ItemRow>
					</evan-form-item>
					<evan-form-item label="涉及系统" prop="aboutSystem">
						<ItemRow v-model="formData.aboutSystem" ref="aboutSystem" dicKey="aboutSystem" chooseType="select-dic-multiple"></ItemRow>
					</evan-form-item>
					<evan-form-item label="分析备注">
						<u-input v-model="formData.remark" type="textarea" input-align="right"></u-input>
					</evan-form-item>
				</evan-form>
			</template>
		</Card>

		<AttachmentCard ref="AttachmentCard"></AttachmentCard>


		<template #footer>
			<HButtonGroup @leftClick="leftClick" @rightClick="rightClick">
				<template #left>
					取消
				</template>
				<template #right>
					提交
				</template>
			</HButtonGroup>
		</template>
	</ListLayout>

</template>
<script>
	import Card from '@/components/card/components/cardBaseWrapper.vue'
	import ListLayout from '@/components/layout/listLayout.vue'
	import HButtonGroup from '@/components/buttonGroup/groupTwo.vue'
	import ItemRow from '@/components/arrow/itemArrow.vue'
	import ProjectApi from '@/apis/project.js'
	import AttachmentCard from '@/components/card/attachment.vue'
	import PagesDemandRouterConfig from '@/pagesDemand/routerConfig.js'
	import { attachmentFormatter } from '@/common/submitFormatter.js'
	export default {
		components: {
			Card,
			ListLayout,
			HButtonGroup,
			ItemRow,
			AttachmentCard
		},

		data() {
			return {
				rules: {
					analyst: [{
						required: true,
						message: '请选择需求分析人',
					}],
					type: [{
						required: true,
						message: '请选择需求类型',
					}],
					coreDemand: [{
						required: true,
						message: '核心需求不能为空',
					}],
					demandValue: [{
						required: true,
						message: '需求价值不能为空',
					}],
					analystTechnical: [{
						required: true,
						message: '请选择技术分析人',
					}],

				},
				formData: {
					// 需求分析人
					analyst: '',
					// 需求类型
					type: '',
					// 核心需求
					coreDemand: '',
					// 需求价值
					demandValue: '',
					// 附件
					enclosure: '',
					// 分析备注 知识库引用id
					remark: '',
					// 技术分析人
					analystTechnical: '',
					remarkId: '',
					itemCode: '',
					aboutSystem: '',
				},


				routerParams: {
					itemCode: ''
				}

			}
		},
		onLoad(query) {
			let { itemCode } = query
			this.routerParams.itemCode = itemCode
			this.initData()

		},
		onReady() {
			this.$nextTick(() => {
				this.$refs.form.setRules(this.rules)

			})
		},
		methods: {
			async initData() {

				let res = await ProjectApi.Demand.GetDemandDetail(this.routerParams.itemCode)
				const data = res.body || {}
				console.log(data, 'data');
				if (data.demandAnalysis) {
					this.formData = data.demandAnalysis;
				} else {
					let userInfo = wx.getStorageSync('pc_userInfo')
					this.formData.analyst = userInfo.id;
					this.formData.type = data.demand.itemType;
					this.formData.itemCode = this.routerParams.itemCode;
					this.formData.aboutSystem = data.demand.aboutSystem || ''
					this.$refs.aboutSystem.resetInit(this.$options.filters.aboutSystem(data.demand.aboutSystem || ''))
					this.$refs.analyst.resetInit(userInfo.userName)
				}
			},

			leftClick() {
				wx.navigateBack()
			},
			rightClick() {
				let self = this;
				self.$refs.form.validate(async (valid) => {

					if (valid) {
						let attachment = this.$refs.AttachmentCard.$data.attachment
						self.formData.enclosure = attachmentFormatter(attachment)
						self.formData.itemCode = self.routerParams.itemCode

						console.log(attachment, self.formData);
						await ProjectApi.Demand.SubmitDemandAnalysis(self.formData);
						wx.redirectTo({
							url: PagesDemandRouterConfig.Demand
						})
					}
				});
			},

		}
	}
</script>
